/*
 * Copyright (C) 2008 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package net.xfok.ui.animation;

import net.xfok.ui.R;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.widget.FrameLayout;
import android.widget.ImageView;



public class Rotate3dImage extends Activity implements OnClickListener,OnTouchListener{
	private static final String TAG="Rotate3dImage";
	private float mCenterX,mCenterY;
	private ImageView img1,img2;
	private RotateCubeAnimation leftAnimation,rightAnimation;
//	private FrameLayout mContainer;
	private int[] images={R.drawable.photo1,R.drawable.photo2,R.drawable.photo3,R.drawable.photo4,R.drawable.photo5,R.drawable.photo6};
	private int id=0;
	private float xStart,xEnd;
	public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.rotate_3d_image); 
        img1=(ImageView)findViewById(R.id.image1);
        img2=(ImageView)findViewById(R.id.image2);
//        mContainer=(FrameLayout)findViewById(R.id.container);
        img1.setOnClickListener(this);
        img2.setOnClickListener(this);
        img1.setOnTouchListener(this);
        img2.setOnTouchListener(this);
//        mContainer.setOnTouchListener(this);
        mCenterX=getWidth(this)/2;
        mCenterY=getHeight(this)/2;      
        img1.setBackgroundResource(images[id%images.length]);
    }

	public static int getWidth(Context cx) {
		DisplayMetrics dm = cx.getApplicationContext().getResources()
				.getDisplayMetrics();
		return dm.widthPixels;
	}

	public static int getHeight(Context cx) {
		DisplayMetrics dm = cx.getApplicationContext().getResources()
				.getDisplayMetrics();
		return dm.heightPixels;
	}
	private void setRight(){
		Log.i(TAG, "id="+id);
		if(id<0)id=id+images.length;
        img1.setBackgroundResource(images[(id)%images.length]);
        img2.setBackgroundResource(images[(id-1+images.length)%images.length]);
        id--;
	}
	private void setLeft(){
        Log.i(TAG, "id="+id);
		if(id<0)id=id+images.length;
        img1.setBackgroundResource(images[id%images.length]);
        img2.setBackgroundResource(images[(id+1)%images.length]);
        id++;
	}
	private void roateRight(){	
		setRight();
		rotate(img1,img2,true);
	}
	
	private void roateLeft(){		
        setLeft();
        rotate(img1,img2,false);
	}
	
	private void rotate(View left,View right,boolean direction){
		int angel=direction?1:-1;
		leftAnimation = new RotateCubeAnimation(0, RotateCubeAnimation.RIGHT_ANGEL*angel, 0, 0, mCenterX, mCenterY);
		rightAnimation = new RotateCubeAnimation((-1)*RotateCubeAnimation.RIGHT_ANGEL*angel, 0, 0, 0, mCenterX, mCenterY);
		leftAnimation.setFillAfter(true);
		leftAnimation.setDuration(RotateCubeAnimation.ROTATE_DURATION);
		rightAnimation.setFillAfter(true);
		rightAnimation.setDuration(RotateCubeAnimation.ROTATE_DURATION);
		left.startAnimation(leftAnimation);
		right.startAnimation(rightAnimation);
	}
	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
//		switch (v.getId()) {
//		case R.id.image1:
//			roateLeft();
//			break;
//		case R.id.image2:	
//			
//			break;
//		default:
//			break;
//		}
	}

	@Override
	public boolean onTouch(View v, MotionEvent event) {
		int action=event.getAction();
		if(action==MotionEvent.ACTION_DOWN){
			xStart=event.getX();
		}
		if(action==MotionEvent.ACTION_UP){
			xEnd=event.getX();
			if(xEnd>xStart){				
				roateRight();
			}else{				
				roateLeft();
			}			
			xEnd=xStart=0;
		}
		// TODO Auto-generated method stub
		return false;
	}
}


